package com.step.ems.pages.home

import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import coil.compose.AsyncImage
import com.step.ems.R
import com.step.ems.common.Constant

@Composable
fun HomePage(
    toElevator: () -> Unit,
    toUrgent: () -> Unit,
    toWb: () -> Unit,
    toSettings: () -> Unit,
) {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.White)
    ) {
        Banner()
        Menu(toElevator, toUrgent, toWb, toSettings)
        Stats(toUrgent, toWb)
    }
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun Banner() {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .height(276.dp)
    ) {
        val pagerState = rememberPagerState(pageCount = {
            Constant.banners.size
        })
        HorizontalPager(
            state = pagerState,
            modifier = Modifier.fillMaxSize()
        ) { page ->
            AsyncImage(
                model = Constant.banners[page],
                contentDescription = "Image",
                modifier = Modifier.fillMaxSize(),
                contentScale = ContentScale.Crop
            )
        }
        Row(
            Modifier
                .wrapContentHeight()
                .fillMaxWidth()
                .align(Alignment.BottomCenter)
                .padding(bottom = 25.dp),
            horizontalArrangement = Arrangement.Center,
        ) {
            repeat(pagerState.pageCount) { iteration ->
                val color =
                    if (pagerState.currentPage == iteration) Color.White else Color.Gray
                Box(
                    modifier = Modifier
                        .height(4.dp)
                        .width(22.dp)
                        .padding(horizontal = 3.dp)
                        .clip(CircleShape)
                        .background(color)
                )
            }
        }
    }
}

@Composable
fun Menu(
    toElevator: () -> Unit,
    toUrgent: () -> Unit,
    toWb: () -> Unit,
    toSettings: () -> Unit,
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .offset(y = (-13).dp)
            .clip(RoundedCornerShape(topStart = 16.dp, topEnd = 16.dp))
            .padding(22.dp, 25.dp, 22.dp, 0.dp)
    ) {
        MenuItem(R.drawable.menu_elevator, R.string.elevator, toElevator)
        MenuItem(R.drawable.menu_repair, R.string.urgent, toUrgent)
        MenuItem(R.drawable.menu_wb, R.string.wb, toWb)
        MenuItem(R.drawable.menu_setting, R.string.setting, toSettings)
    }
}

@Composable
fun RowScope.MenuItem(@DrawableRes iconId: Int, @StringRes textId: Int, to: () -> Unit) {
    val text = stringResource(textId)
    Column(
        modifier = Modifier
            .weight(1f)
            .clickable { to() }
            .padding(top = 20.dp, bottom = 20.dp),
        verticalArrangement = Arrangement.Top,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Image(
            modifier = Modifier.height(48.dp),
            painter = painterResource(id = iconId),
            contentDescription = text
        )
        Text(
            text = text,
            fontSize = 14.sp,
            maxLines = 1,
            overflow = TextOverflow.Ellipsis
        )
    }
}

@Composable
fun Stats(
    toUrgent: () -> Unit,
    toWb: () -> Unit,
    viewModel: HomeViewModel = hiltViewModel(),
) {
    val urgentNum by viewModel.urgentNum.collectAsStateWithLifecycle()
    val wbNum by viewModel.wbNum.collectAsStateWithLifecycle()

    Row(
        modifier = Modifier
            .padding(horizontal = 20.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        StatsItem(stringResource(R.string.urgentNum), urgentNum, toUrgent)
        Spacer(modifier = Modifier.width(13.dp))
        StatsItem(stringResource(R.string.wbNum), wbNum, toWb)
    }
}

@Composable
fun RowScope.StatsItem(title: String, value: String, to: () -> Unit) {
    Column(
        modifier = Modifier
            .border(1.dp, Color(0xFFE8E8E8), RoundedCornerShape(10.dp))
            .clickable { to() }
            .padding(horizontal = 13.dp, vertical = 8.dp)
            .weight(1f),
    ) {
        Text(
            text = title,
            color = Color.Gray,
            fontSize = 15.sp,
        )
        Text(
            modifier = Modifier.padding(top = 5.dp),
            text = value,
            color = Color(0xFF444444),
            fontSize = 23.sp,
            fontWeight = FontWeight.Bold
        )
    }
}